<?php
require_once LIBRARY_PATH. '/Twig/LoaderInterface.php';
class ZExt_Twig_Loader extends Twig_LoaderInterface
{

	/**
	 * @var string
	 */
	private $_applicationRoot = null;

	/**
	 * @var string
	 */
	private $_modulesDir = null;

	/**
	 * @var string
	 */
	private $_viewsDir = null;

	/**
	 * Constructor.
	 *
	 * @param string	$applicationRoot The application root.
	 * @param string	$modulesDir The directory where all modules are found.
	 * @param string	$viewsDir The path where views should be located in both the application root and
	 * modules directory.
	 * @param string 	$cache The compiler cache directory.
	 * @param boolean	$autoReload Whether to reload the template if the orginal source changed.
	 *
	 * @see Twig_Loader
	 */
	public function __construct($applicationRoot, $modulesDir, $viewsDir = "/views/scripts",
		$cache = null, $autoReload = true)
	{
		$this->setApplicationRoot($applicationRoot);
		$this->setModulesDir($modulesDir);
		$this->setViewsDir($viewsDir);

		parent::__construct($cache, $autoReload);
	}

	/**
	 * Returns the source of the next view to load based on the supplied name.
	 * The name in this instance should take the form 'module/controller/action',
	 * 'controller/action', or just 'action'.
	 *
	 * @param string	$name The view to load.
	 *
	 * @return array An array consisting of the source code as the first element, and
	 * the last modification time as the second one or false if it's not relevant.
	 */
	public function getSource($name)
	{
		$name = $this->_parseName($name);
		$templateFilePath = $this->getApplicationRoot();
		if (!is_null($name['module']))
		{
			$templateFilePath .= DIRECTORY_SEPARATOR.$name['module'];
		}
		$templateFilePath .= DIRECTORY_SEPARATOR.$this->getViewsDir();
		$templateFilePath .= DIRECTORY_SEPARATOR.$name['controller'].DIRECTORY_SEPARATOR.$name['action'].$name['extension'];

		if (!file_exists($templateFilePath))
		{
			throw new Zend_Exception("The template file $templateFilePath does not exist.");
		}

		error_log($templateFilePath);
		return array(file_get_contents($templateFilePath), filemtime($templateFilePath));

	}

	public function getViewsDir()
	{
		return $this->_viewsDir;
	}

	public function setViewsDir($viewsDir)
	{
		$this->_viewsDir = $viewsDir;
	}

	public function getApplicationRoot()
	{
		return $this->_applicationRoot;
	}

	public function setApplicationRoot($applicationRoot)
	{
		$this->_applicationRoot = $applicationRoot;
	}

	public function getModulesDir()
	{
		return $this->_modulesDir;
	}

	public function setModulesDir($modulesDir)
	{
		$this->_modulesDir = $modulesDir;
	}

	private function _parseName($name)
	{
		$matches = array();
		preg_match("/(\..*)$/", $name, $matches);
		$name = substr($name, 0, strpos($name, $matches[1]));
		$parsedName = array('module' => null, 'controller' => 'index', 'action' => 'index', 'extension' => $matches[1]);
		$parts = split("/", $name);
		switch (sizeof($parts))
		{
			case 3:
				$parsedName['module'] = array_shift($parts);
			case 2:
				$parsedName['controller'] = array_shift($parts);
			case 1:
				$parsedName['action'] = array_shift($parts);
		}

		return $parsedName;
	}
}
